package com.mdtx.controller;

import com.mdtx.base.Path;
import com.mdtx.base.ResultData;
import com.mdtx.database.AbstractDatabaseOperation;
import com.mdtx.database.db.MysqlDataBaseOperation;
import com.mdtx.database.db.OracleDataBaseOperation;
import com.mdtx.database.type.DbType;
import com.mdtx.entity.*;
import com.mdtx.utils.FileUtil;
import com.mdtx.write.GenerateCode;
import com.mdtx.write.TemplateName;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 猫啊
 * @create 2018-06-13 13:39
 **/
@RestController
public class GenController {


    @Autowired
    private MysqlDataBaseOperation mysql;
    @Autowired
    private OracleDataBaseOperation oracle;
    @Autowired
    private GenerateCode generate;


    @RequestMapping(value = "/dbs",method = RequestMethod.POST)
    public ResultData<List<String>> loadMysqlDbsList(@RequestBody DbConfig dbConfig){
        String sql ="";
        List<String> dbs = null;
        if(dbConfig.getDbType().equals(DbType.MYSQL)){
            sql ="show databases";
            mysql.setDbConfig(dbConfig);
            dbs =mysql.queryAllDatabases(sql);
        }else if(dbConfig.getDbType().equals(DbType.ORACLE)){
            sql ="SELECT owner FROM ALL_TABLES GROUP BY owner";
            oracle.setDbConfig(dbConfig);
            dbs =oracle.queryAllDatabases(sql);
        }

        return new ResultData<List<String>>(0,"success",dbs);
    }

    public AbstractDatabaseOperation getData(String dbType){
        if(dbType.equals(DbType.MYSQL)){
            return mysql;
        }else if(dbType.equals(DbType.ORACLE)){
            return oracle;
        }
        return null;

    }



    @RequestMapping(value = "/tables/{dbType}/{db}",method = RequestMethod.GET)
    public ResultData<List<ClassAttribute>> loadTablesList(@PathVariable String dbType,@PathVariable String db){

        this.getData(dbType).configDb(db);
        List<ClassAttribute> tables = this.getData(dbType).queryDatabaseAllTables(db);
        return new ResultData<List<ClassAttribute>>(0,"success",tables);
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public ResultData generateCode(@RequestBody TableVo tables){

        Map<String, Object> root = null;

        ResultData resultData = new ResultData();

        //文件名称
        String poName="",mapperName="",daoName="";

        String db=this.getData(tables.getDbType()).getDbConfig().getDb(),
                dbType=tables.getDbType();

        for (TableAttribute table : tables.getTables()) {
            root = new HashMap<String, Object>();

            ClassAttribute classAttribute = new ClassAttribute();
            classAttribute.setTableName(table.getTableName());
            classAttribute.setComment(table.getComment());
            classAttribute.setPackagePath(tables.getPackagePath());
            classAttribute.setAuthor(tables.getAuthor());
            classAttribute.setDate(new Date());
            classAttribute.setComment(table.getComment());
            //字段
            List<FieldAttribute> list = this.getData(dbType).queryTableColumns(db,table.getTableName());
            //主键
            List<Map<String,String>> keys = this.getData(dbType).queryTablePrimaryKeys(db,table.getTableName());

            root.put("classAttribute",classAttribute);
            root.put("fieldAttributeList",list);
            root.put("keys",keys);

            poName = classAttribute.getClassName()+".java";
            mapperName = classAttribute.getClassName()+"Mapper.xml";
            daoName = classAttribute.getClassName()+"Mapper.java";

            try {
                tables.setPath("D:\\");

                //生成文件夹
                String path = FileUtil.assemblePath(tables.getPackagePath(),tables.getPath());

                FileUtil.mkdir(path+ Path.ENTITY_PO);
                FileUtil.mkdir(path+Path.MYBATIS_MAPPER);
                FileUtil.mkdir(path+Path.DAO);

                generate.generate(root, TemplateName.MODEL,path+Path.ENTITY_PO+"\\"+poName);
                generate.generate(root, TemplateName.MYBATIS_MAPPER_XML,path+Path.MYBATIS_MAPPER+"\\"+mapperName);
                generate.generate(root, TemplateName.DAO,path+Path.DAO+"\\"+daoName);
                resultData.setCode(1);
                resultData.setMessage("生成成功");
            } catch (TemplateException e) {
                e.printStackTrace();
                resultData.setCode(1);
                resultData.setMessage("生成失败");
            }

        }

        return resultData;

    }
}
